/**
 * 
 */
package com.iss.card.common;

import android.util.Log;

/**
 * @author dtdh
 * 
 */
public class CardTypeUtil {
	private final static String TAG = CardTypeUtil.class.getSimpleName();
	public static final int CARD_TYPE_NUMBER = 1 << 0; // 0x01
	public static final int CARD_TYPE_VALUE = 1 << 1; // 0x02
	public static final int CARD_TYPE_POINTES = 1 << 2; // 0x04
	public static final int CARD_TYPE_DISCOUNT = 1 << 3; // 0x08

	public static final String STR_CARD_TYPE_NUMBER = "1";
	public static final String STR_CARD_TYPE_MULTIPLE = "2";

	public static final String STR_CARD_ATTR_POINTS = "1";
	public static final String STR_CARD_ATTR_VALUE = "2";
	public static final String STR_CARD_ATTR_DISCOUNT = "3";

	public static boolean isCardTypeNumber(int data) {
		return (data & CARD_TYPE_NUMBER) == CARD_TYPE_NUMBER;
	}

	public static boolean isCardTypeMoney(int data) {
		return (data & CARD_TYPE_VALUE) == CARD_TYPE_VALUE;
	}

	public static boolean isCardTypeScore(int data) {
		return (data & CARD_TYPE_POINTES) == CARD_TYPE_POINTES;
	}

	public static boolean isCardTypeDiscount(int data) {
		return (data & CARD_TYPE_DISCOUNT) == CARD_TYPE_DISCOUNT;
	}

	public static int getCardTypeByAttributes(String type, String attrs) {
		Log.d(TAG, "getCardTypeByAttributes: type=" + type + ", attrs=[" + attrs + "]");
		int myType = 0;
		if (STR_CARD_TYPE_NUMBER.equals(type)) {
			myType = CARD_TYPE_NUMBER;
		} else if (STR_CARD_TYPE_MULTIPLE.equals(type)) {
			if (attrs.contains(STR_CARD_ATTR_POINTS)) {
				myType = CARD_TYPE_POINTES;
			}
			
			if (attrs.contains(STR_CARD_ATTR_VALUE)) {
				myType |= CARD_TYPE_VALUE;
			}
			
			if (attrs.contains(STR_CARD_ATTR_DISCOUNT)) {
				myType |= CARD_TYPE_DISCOUNT;
			}
		}

		Log.d(TAG, "getCardTypeByAttributes:" + myType);
		return myType;
	}

	public static int getCardTypeAttributeNum(int data) {
		int x = data;
		x = (x & 0x5555) + ((x >> 1) & 0x5555);
		x = (x & 0x3333) + ((x >> 2) & 0x3333);
		x = (x & 0x0f0f) + ((x >> 4) & 0x0f0f);
		x = (x & 0x00ff) + ((x >> 8) & 0x00ff);
		return (x);
	}
}
